package com.wzt.controller;

import com.wzt.pojo.Books;
import com.wzt.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController{

        @Autowired
        @Qualifier("bookServiceImpl")
        private BookService bookService;

        @RequestMapping("/allBook")
        public String list(Model model) {
            List<Books> list = bookService.queryAllBook();
            model.addAttribute("list", list);
            return "allBook";
            }


            //跳转到添加书籍页面
            @RequestMapping("/toAddBook")
            public String toAddPaper(){

                return "addBook";
            }


            //添加书籍
            @RequestMapping("/addBook")
            public String addBook(Books books){
                bookService.addBook(books);
                //重定向到allBook
                return "redirect:/book/allBook";
            }

            //跳转到跟新页面
            @RequestMapping("/toUpdate")
            public String toUpdatePaper(int id,Model model){
                Books book = bookService.queryBookById(id);
                model.addAttribute("Qbook",book);
                return "updateBook";
            }

            //对数据进行修改
            @RequestMapping("/updateBook")
            public String updateBook(Books books){
                bookService.updateBook(books);
                return "redirect:/book/allBook";
            }

            //删除书籍
            @RequestMapping("/deleteBook/{bookId}")
            public String deleteBook(@PathVariable("bookId") int id){
                bookService.deleteBookById(id);
                return "redirect:/book/allBook";
            }

            //查询书籍
            @RequestMapping("/queryBook")
            public String queryBook(String queryBookName,Model model){
                Books books = bookService.queryBookByName(queryBookName);
                //创建集合存放查到的书籍
                List<Books> list=new ArrayList<Books>();
                list.add(books);
                //判断
                if(books==null){
                    list=bookService.queryAllBook();
                    model.addAttribute("error","未查到");
                }
                model.addAttribute("list",list);
                return "allBook";
            }

}
